package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.service.PurchaseListService;
import com.atguigu.jxc.service.SupplierService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 进货管理
 */
@RestController
@RequestMapping("/purchaseListGoods")
public class PurchaseListController {

    @Autowired
    private PurchaseListService purchaseListService;

    //http://localhost:8080/purchaseListGoods/savePurchaseList?purchaseNumber=JH1605768306735
    //新增进货单
    @PostMapping("/save")
    public ServiceVO savePurchaseList(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session){
        try {
            purchaseListService.savePurchaseList(purchaseList,purchaseListGoodsStr,session);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/purchaseListGoods/list
    //进货单据查询
    //进货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
    @PostMapping("/list")
    public Map<String,Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime){
       return purchaseListService.list(purchaseNumber,supplierId,state,sTime,eTime);
    }


    //http://localhost:8080/purchaseListGoods/goodsList
    //查询进货单商品信息  根据purchaseListId
    @PostMapping("/goodsList")
    public Map<String,Object> goodsList(Integer purchaseListId){
        return purchaseListService.goodsList(purchaseListId);
    }

    //http://localhost:8080/purchaseListGoods/delete
    //进货单删除
    @PostMapping("/delete")
    public ServiceVO delete(Integer purchaseListId){
        try {
            purchaseListService.delete(purchaseListId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/purchaseListGoods/updateState
    //支付结算（修改进货单付款状态） Integer purchaseListId
    @PostMapping("/updateState")
    public ServiceVO updateState(Integer purchaseListId){
        try {
            purchaseListService.updateState(purchaseListId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/purchaseListGoods/count
    //进货统计（可根据 商品类别、商品编码或名称 条件查询）
    @PostMapping("/count")
    public String count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        return purchaseListService.count(sTime,eTime,goodsTypeId,codeOrName);
    }
}
